//
//  LoginView.swift
//  AirlineReservation
//
//  Created by Kevin on 2020/1/26.
//  Copyright © 2020 Kevin. All rights reserved.
//

import UIKit

public protocol LoginViewDelegate {
    
    func clickFotPwd()
    func clickRegUser()
    func selectServer()
    func selectFakeData()
    func clickLoginSwitchBtn()
    func clicklogin(mobile:String?, password:String?)
}

class LoginView: UIView {
    
    // 密码输入
    private lazy var inputPwd = InputTextView(title: "登录密码")
    // 账号输入
    private lazy var inputName = InputTextView(title: "手机号")
    // 切换登录方式
    public lazy var moreLabel = UILabel(title: "切换登录方式", color: UIColor.darkGray, size: 14,align: .right)
    
    // MARK: 代理
    public var delegate:LoginViewDelegate?
    
    public var resetMoreLableShow:Bool? {
        didSet {
            moreLabel.isHidden = resetMoreLableShow ?? false
        }
    }
    
    override func layoutSubviews() {
        if moreLabel.frame.origin.x > 0 {
            return
        }
        // 提示文本
        let titleTip = UILabel(title: "欢迎使用航票票", color: COLOR_NICE,size: MARGIN30,align: .center)
        titleTip.frame = CGRect(x: 0, y: frame.height * 0.15, width: frame.width, height: MARGIN40)
        addSubview(titleTip)
        // 手势事件 左滑
        let gesture1 = UISwipeGestureRecognizer(target: self, action: #selector(selectConfig(gesture:)))
        gesture1.direction = .right
        titleTip.addGestureRecognizer(gesture1)
        // 手势事件 右滑
        let gesture2 = UISwipeGestureRecognizer(target: self, action: #selector(selectConfig(gesture:)))
        gesture2.direction = .left
        titleTip.addGestureRecognizer(gesture2)
        // 账号输入
        let inputW = frame.width - MARGIN * 2
        inputName.keyboardType = .numberPad
        inputName.frame = CGRect(x: MARGIN, y: titleTip.frame.maxY + MARGIN35, width: inputW, height: MARGIN50)
        addSubview(inputName)
        // 密码输入
        inputPwd.frame = CGRect(x: MARGIN, y: inputName.frame.maxY + MARGIN, width: inputW, height: MARGIN50)
        addSubview(inputPwd)
        // 登录按钮
        let loginBtn = UIButton(title: "登录", name: "button_orange")
        loginBtn.frame = CGRect(x: MARGIN, y: inputPwd.frame.maxY + MARGIN30, width: inputW, height: MARGIN50)
        addSubview(loginBtn)
        // 登录按钮 点击事件
        loginBtn.addTarget(self, action: #selector(clicklogin), for: .touchUpInside)
        // 忘记密码
        let forgetPwd = UILabel(title: "忘记密码", color: UIColor.lightGray)
        forgetPwd.frame = CGRect(x: MARGIN, y: loginBtn.frame.maxY + MARGIN, width: MARGIN100, height: MARGIN)
        addSubview(forgetPwd)
        // 忘记密码 点击事件
        forgetPwd.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(clickFotPwd)))
        // 注册账号
        let createAccount = UILabel(title: "注册账号",color: UIColor.lightGray,align: .right)
        let createAccountY = loginBtn.frame.maxY + MARGIN
        let createAccountX = frame.width - MARGIN - MARGIN100
        createAccount.frame = CGRect(x: createAccountX, y: createAccountY, width: MARGIN100, height: MARGIN)
        addSubview(createAccount)
        // 注册账号 点击事件
        createAccount.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(clickRegUser)))
        // 切换登录方式
        let moreX = frame.width - MARGIN100 - MARGIN
        moreLabel.frame = CGRect(x: moreX, y: frame.height - MARGIN40, width: MARGIN100, height: MARGIN30)
        addSubview(moreLabel)
        moreLabel.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(clickSwitchBtn)))
    }
    
    @objc func clickFotPwd() {
        delegate?.clickFotPwd()
    }
    
    @objc func clickRegUser() {
        delegate?.clickRegUser()
    }
    
    @objc func clicklogin() {
        delegate?.clicklogin(mobile: inputName.text, password: inputPwd.text)
    }
    
    @objc func clickSwitchBtn() -> Void {
        delegate?.clickLoginSwitchBtn()
    }
    
    @objc func selectConfig(gesture:UISwipeGestureRecognizer) -> Void {
        if gesture.direction == .right {
            delegate?.selectServer()
        }
        if gesture.direction == .left {
            delegate?.selectFakeData()
        }
    }
    
}
